package relativeSortArray;

public class Solution {
    public int[] relativeSortArray(int[] arr1, int[] arr2) {
        //
        int[] map = new int[1001];
        for (int i = 0; i < arr1.length; i++) {
            map[arr1[i]]++; // 统计每个元素出现的次数
        }
        int index = 0;
        int[] ans = new int[arr1.length];
        for (int v: arr2) {
            while (map[v]>0) {
                ans[index++] = v;
                map[v]--;
            }
        }
        // 根据大小排序
        for (int i = 0; i < map.length; i++) {
            while (map[i]>0) {
                ans[index++] = i;
                map[i]--;
            }
        }
        return ans;
    }
}
